package com.dragon.graduation.role.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.dragon.util.MyPage;
import com.dragon.util.WrapperUtil;
import com.dragon.graduation.role.controller.dto.RoleAddDTO;
import com.dragon.graduation.role.controller.dto.RoleSearchDTO;
import com.dragon.graduation.role.controller.dto.RoleUpdateDTO;
import com.dragon.graduation.role.controller.vo.RoleVO;
import com.dragon.graduation.role.entity.RoleEntity;
import com.dragon.graduation.role.service.RoleService;
import com.dragon.config.exception.exception.GlobalException;
import com.dragon.results.page.ResultPage;
import com.dragon.results.result.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author dragon
 * @since 2025-02-28 21:21:11
 */
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
@Api(tags = "角色表 相关接口")
public class RoleController {

    private final RoleService roleService;

    @PostMapping("add")
    @ApiOperation("1]角色表添加")
    @ApiOperationSupport(order = 1)
    public HttpResult<String> add(@RequestBody RoleAddDTO addDTO) {
        RoleEntity entity = RoleAddDTO.to(addDTO, RoleEntity.class);
        roleService.save(entity);
        return HttpResult.ok("添加成功");
    }

    @PostMapping("update")
    @ApiOperation("2]角色表修改")
    @ApiOperationSupport(order = 2)
    public HttpResult<String> update(@RequestBody RoleUpdateDTO updateDTO) {
        RoleEntity entity = roleService.getById(updateDTO.getId());
        Assert.isFalse(ObjectUtils.isEmpty(entity), () -> new GlobalException("要修改的数据不存在"));
        entity = RoleUpdateDTO.to(updateDTO, RoleEntity.class);
        roleService.updateById(entity);
        return HttpResult.ok("修改成功");
    }

    @PostMapping("search")
    @ApiOperation("3]角色表分页查询")
    @ApiOperationSupport(order = 3)
    public HttpResult<ResultPage<List<RoleVO>>> search(@RequestBody RoleSearchDTO searchDTO) {
        QueryWrapper<RoleEntity> queryWrapper = WrapperUtil.constructWrapper(searchDTO, RoleEntity.class);

        Page<RoleEntity> entityPage = MyPage.setPage(RoleEntity.class, searchDTO.getPage());
        Page<RoleEntity> page = roleService.page(entityPage, queryWrapper);
        ResultPage<List<RoleVO>> resultPage = MyPage.copyPage(page, RoleVO.class);
        return HttpResult.okForData(resultPage);
    }

    @PostMapping("delete")
    @ApiOperation("4]角色表删除")
    @ApiOperationSupport(order = 4)
    public HttpResult<String> delete(@RequestBody List<Integer> idList) {
        if (idList.contains(1)||idList.contains(2)||idList.contains(3)) {
            throw new GlobalException("内置角色不可删除！！！");
        }
        roleService.removeByIds(idList);
        return HttpResult.ok("删除成功");
    }
}
